package com.app.controller;

import com.app.common.Result;
import com.app.common.SystemConstant;
import com.app.mapper.FileMapper;
import com.app.model.FileModel;
import com.app.model.RecordFileModel;
import com.app.model.TimeWarningModel;
import com.app.server.TimeWarningService;
import com.app.utils.CommonHelp;
import com.app.vo.TimeWarningVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 实时预警
 */
@Slf4j
@RestController
@RequestMapping("/sbbz")
public class TimeWarningController {
    @Autowired
    private TimeWarningService timeWarningService;
    @Autowired
    private FileMapper fileMapper;

    // 查询列表
    @GetMapping("/getWarnList")
    public Result getTimeWarningList(@RequestParam(defaultValue = "0") Integer page,
                                     @RequestParam(defaultValue = "10") Integer limit,
                                     String title) {
        TimeWarningVo timeWarningList = timeWarningService.getTimeWarningList(page, limit, title);
        return Result.success(timeWarningList);
    }

    //根据id查询
    @GetMapping("/getWarnById")
    public Result<Object> getTimeWarningById(Integer id) {
        TimeWarningModel timeWarning = timeWarningService.getById(id);
        return Result.success(timeWarning);
    }

    // 新增和修改
    @PostMapping("/addAndUpdateWarn")
    public Result addAndUpdateTimeWarning(@RequestBody TimeWarningModel timeWarningModel) {
        if (timeWarningModel.getPdf() != null) {
            QueryWrapper<FileModel> fileModelQueryWrapper = new QueryWrapper<>();
            fileModelQueryWrapper.eq("url", timeWarningModel.getPdf());
            FileModel fileModel = fileMapper.selectOne(fileModelQueryWrapper);
            if (fileModel != null) {
                timeWarningModel.setPdfName(fileModel.getName());
            }
        }
        if (timeWarningModel.getId() != null && timeWarningModel.getId() > 0) {
            timeWarningModel.setUpdateTime(CommonHelp.getNowTimestamp());
            timeWarningService.updateById(timeWarningModel);
            return Result.success("修改成功");
        } else {
            timeWarningModel.setCreateTime(CommonHelp.getNowTimestamp());
            timeWarningService.save(timeWarningModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delWarn")
    public Result delTimeWarning(Integer id) {
        timeWarningService.removeById(id);
        return Result.success("删除成功");
    }

    //接收数据并保存
    @PostMapping("/getWarn")
    public Result<Object> getWarning(@RequestBody TimeWarningModel warningModel) {
        if (warningModel.getArea().equals("肃北")) {
            timeWarningService.save(warningModel);
        }
        return Result.success("操作成功");
    }

    // 查询列表
    @GetMapping("/selectWarningList")
    public Result<Object> selectWarningList(Integer page, Integer limit, String title) {
        TimeWarningVo timeWarningList = timeWarningService.getTimeWarningList(page, limit, title);
        return Result.success(timeWarningList);
    }
}
